<?php
/**
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;
class Ad extends Common
{
    public function index()
    {
    	$muban = array();
        $mubans = get_115cms(ROOT_PATH . 'app/template/pc/' . DS);
        foreach ($mubans as $name) {
            $muban[] = $this->cms_pc($name);
        }
        $this->assign('muban', $muban);
        $mubanw = array();
        $mubansw = get_115cms(ROOT_PATH . 'app/template/wap/' . DS);
        foreach ($mubansw as $name) {
            $mubanw[] = $this->cms_wap($name);
        }
        $this->assign('mubanw', $mubanw);
        return view();
    }
    //添加
    public function insert()
    {
        $data = $this->request->post();
        $data['time'] = time();
        $data['times'] = strtotime(input('times'));
        if (!Db::name('ad')->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //更新
    public function update()
    {
        $id = input('id');
        $data = $this->request->post();
        $data['times'] = strtotime(input('times'));
        if (Db::name('ad')->where('id', $id)->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //pc模板获取
    public function cms_pc($name)
    {
        $url = DS . 'app/template/pc/' . DS;
        $info = array();
        if (is_dir(ROOT_PATH . $url . $name . DS)) {
            $info = (include ROOT_PATH . $url . $name . DS . 'Cms.php');
            $info['name'] = $name;
            return $info;
        }
    }
    public function cms_wap($name)
    {
        $url = DS . 'app/template/wap/' . DS;
        $info = array();
        if (is_dir(ROOT_PATH . $url . $name . DS)) {
            $info = (include ROOT_PATH . $url . $name . DS . 'Cms.php');
            $info['name'] = $name;
            return $info;
        }
    }
    public function cate()
    {
    	$muban = array();
        $mubans = get_115cms(ROOT_PATH . 'app/template/pc/' . DS);
        foreach ($mubans as $name) {
            $muban[] = $this->cms_pc($name);
        }
        $this->assign('muban', $muban);
        $mubanw = array();
        $mubansw = get_115cms(ROOT_PATH . 'app/template/wap/' . DS);
        foreach ($mubansw as $name) {
            $mubanw[] = $this->cms_wap($name);
        }
        $this->assign('mubanw', $mubanw);
        return view();
    }
    //添加分类
    public function insertcate()
    {
        $data = $this->request->post();
        if (fun_st('ad_cate',array('in'=>input('in')))!=0) {
            $this->error('当前模板的广告位存在');
        }
        
        $data['time'] = time();
        if (!Db::name('ad_cate')->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //更新分类
    public function updatecate()
    {
        $id = input('id');
        $data = $this->request->post();
        if (Db::name('ad_cate')->where('id', $id)->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //广告分享页面
    public function sql()
    {
        return view();
    }
}